import { KeywordModel } from '../../../models/keyword.js'
import { paginationBev } from '../../behaviors/pagination.js'

const keywordModel = new KeywordModel()

Component({
  /**
   * 组件的属性列表
   */
  behaviors: [paginationBev],
  properties: {
    hotWords: Array,
    more: {
      type: String,
      observer: 'loadMore'
    },
    q:{
      type: String,
      default:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    searching: false,
    // loading: false,
    loadingCenter: false
  },

  attached() {
    this.setData({
      historyWords: keywordModel.getHistory()
    })
  },


  /**
   * 组件的方法列表
   */
  methods: {
    onCancel(event) {
      // this.initialize()
      this.triggerEvent('cancle', {}, {})
    },

    clear(event) {
      wx.clearStorage()
      this.setData({
        historyWords: []
      })
    },

    onConfirm: function(event) {
      console.log(event)
      this._showResult()
      this._showLoadingCenter()

      const q = event.detail.value || event.currentTarget.dataset.id
      console.log(q)
      this.setData({
        q
      })
      keywordModel.addToHistory(q)
      keywordModel.addHotWord(q)
      //执行后台查询
      this.triggerEvent('onConfirm', {
        q:this.data.q
      }, {})
      this._hideLoadingCenter()
    },
    _showLoadingCenter() {
      this.setData({
        loadingCenter: true
      })
    },

    _hideLoadingCenter() {
      this.setData({
        loadingCenter: false
      })
    },

    onDelete: function(event) {
      this._closeResult()
      this.setData({
        searching: false,
        empty:false,
      })
    },

    _showResult() {
      this.setData({
        searching: true
      })
    },
    _closeResult() {
      this.setData({
        searching: false,
        q: ''
      })
    }
  }
})
